<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../css/together.css" rel="stylesheet"  />
	</head>
	
	<body>
		<header class="mui-bar mui-bar-nav m_barC">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
		    <h1 class="mui-title">渭书收银台</h1>
		</header>
		<div class="mui-content">
			<div class="paystop">
				支付方式
			</div>
			
				<div class="paysbottom  mui-input-row mui-radio mui-right">
						<label class="paysbl ">
								<img class="weiimg" src="../images/wx2.png" alt="" />
								<div>
									微信支付
									<span>微信安全支付</span>
								</div>
						</label>
					<input type="radio" checked="checked"/>
				</div>
			</div>
			<div class="wxpay" id="btn_pay">
				微信支付0.01元
			</div>
		<script src="../js/jquery.min.js" type="text/javascript" charset="utf-8"></script>	
		<script src="../js/mui.min.js"></script>
		<script src="../js/req.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/muishow.js" type="text/javascript" charset="utf-8"></script>
		
		<script type="text/javascript">
			 var channel = null;	//支付通道
			 
			 
			window.onload = function(){
				mui.init();
				mui.plusReady(function(){
                //定义支付通道
               // getChannels();
                //获取支付通道
              var data = {
              	"appid":"wx0411fa6a39d61297",
              	"noncestr":"KlkeCEaMcUHgMOua",
              	"package":"Sign=WXPay",
              	"partnerid":"1230636401",
              	"prepayid":"wx301512318115887d4f4f12ce1910775533",
              	"timestamp":1553929951,
              	"sign":"125C4253FEE3F16CD82E24C7ACA7E2DA"
              	}
               
                //微信支付按钮 发起微信支付
                document.getElementById("btn_pay").addEventListener("tap",function(){
                    pay("",data);
                })
            });
				 
				 
			}
			
			 //获取支付通道的方法
			 function getChannels(){
                    plus.payment.getChannels(function(channels) {
                        for (var i = 0; i < channels.length; i++) {
                            if (channels[i].id == "wxpay") {
                                channel = channels[i];
                            }
                        }
                        //plus.ui.toast("使用支付方式:" + channel.id);
                    }, function(e) {
                        plus.ui.toast("获取支付通道失败!");
                        console.log("获取支付通道失败!");
                    })
            }
			
			/**
             * 支付
             * @param {Object} payType  支付类型
             * @param {Object} payStatement  调起支付宝或微信的statment支付订单信息
             */
            function pay(payType, payStatement) {
                /***判断支付通道****/
                //最终的支付通道
                var channel;
                /***
                 * 用于标识支付通道： 
                 * "alipay" - 表示支付宝；
                 * "wxpay" - 表示微信支付； 
                 */
                var payId; //支付标识
                if(payType == "ALIPAY_ANDROID") {
                    payId = "alipay";
                } else {
                    payId = "wxpay";
                }

                // 取出支付宝和微信的支付通道
                plus.payment.getChannels(function(channels) {
                    mui.each(channels, function(index, element) {
                        if(element.id == payId) {
                            channel = element;
                        }
                    });
                    if(!channel) {
                        mui.toast('获取支付通道失败，请重试！');
                    }
                    //发起支付
                    plus.payment.request(channel, payStatement, function(result) {
                        mui.toast('支付完成');
                        /**
                         *查询订单状态是否已支付
                         * 轮询查询订单状态
                         * 
                         */
                        

                    }, function(error) {
                        console.log(JSON.stringify(error));
                        mui.toast("支付失败");
                    });

                }, function(e) {
                    mui.toast("获取支付通道列表失败：" + e.message);
                });
            }
			
                
                /*
                 
                 
                 var timer1 = setInterval(function() {
                            app.tokenAjax_Get({
                                url: API_URL_GET_ORDER_STATUS + dataSource.orderId,
                                success: function(result) {
                                    if(result.status == 1) {
                                        var status = result.data;
                                        if(status == "JUST_CREATED") {
                                            mui.toast('订单未支付');
                                        } else if(status == 'CANCEL') {
                                            mui.toast('订单已被取消');
                                            //清除定时器
                                            clearInterval(timer);
                                            clearInterval(timer1)
                                            //打开
                                            plus.webview.currentWebview().close();
                                            plus.webview.getWebviewById("pay").close();
                                            plus.webview.getWebviewById("order").close();
                                        } else {
                                            //清除定时器
                                            clearInterval(timer);
                                            clearInterval(timer1)
                                            //打开
                                            app.openRefreshOrderListPage();
                                        }
                                    }
                                },
                                error: function(xhr) {
                                    app.httpError(xhr.status);
                                }
                            });
                        }, 1000);
                 
                 setTimeout(function() {
                        mui.confirm('支付已完成', '提示', ['支付遇到问题', '支付完成'], function(e) {
                            if(e.index == 1) {
                                app.tokenAjax_Get({
                                    url: API_URL_GET_ORDER_STATUS + dataSource.orderId,
                                    success: function(result) {
                                        if(result.status == 1) {
                                            var status = result.data;
                                            if(status == "JUST_CREATED") {
                                                mui.toast('订单未支付');
                                            } else if(status == 'CANCEL') {
                                                mui.toast('订单已被取消');
                                                //清除定时器
                                                clearInterval(timer);
                                                //打开
                                                plus.webview.currentWebview().close();
                                                plus.webview.getWebviewById("pay").close();
                                                plus.webview.getWebviewById("order").close();
                                            } else {
                                                //清除定时器
                                                clearInterval(timer);
                                                //打开
                                                app.openRefreshOrderListPage();
                                            }
                                        }
                                    },
                                    error: function(xhr) {
                                        app.httpError(xhr.status);
                                    }
                                });
                            }
                        }, 'div')
                    }, 3000);
                 
                 */
               
		</script>
	</body>

</html>